package com.zfj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zfj.entity.EduChapter;
import com.zfj.entity.EduVideo;
import com.zfj.entity.chapter.ChapterVo;
import com.zfj.entity.vo.CoursePublishVo;
import com.zfj.handler.GuLiException;
import com.zfj.service.EduChapterService;
import com.zfj.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zfj
 * @since 2022-10-07
 */
@CrossOrigin
@RestController
@RequestMapping("/eduService/edu-chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    //课程大纲列表,根据课程id进行查询
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list=chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    // 添加章节
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter chapter){

        boolean result = chapterService.save(chapter);
        if (!result){
            throw new GuLiException(20001, "添加章节失败");
        }

        return R.ok();
    }

    // 根据id查询章节
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable("chapterId") String chapterId){

        EduChapter chapter = chapterService.getById(chapterId);

        return R.ok().data("chapter", chapter);
    }

    // 修改章节
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter){

        boolean result = chapterService.updateById(chapter);
        if (!result){
            throw new GuLiException(20001, "修改章节失败");
        }

        return R.ok();
    }

    // 删除
    @DeleteMapping("{chapterId}")
    public R deleteById(@PathVariable("chapterId") String chapterId){
        boolean flag=chapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }

    }



}

